package com.example.dw.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.dw.entity.UserStudyPeriod;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 用户学习课时Mapper接口
 * @author system
 * @since 2024-01-01
 */
@Mapper
public interface UserStudyPeriodMapper extends BaseMapper<UserStudyPeriod> {

    /**
     * 根据用户ID和课时ID获取学习记录
     */
    @Select("SELECT * FROM user_study_period " +
            "WHERE userid = #{userid} AND periodid = #{periodid}")
    UserStudyPeriod getUserStudyPeriod(@Param("userid") Integer userid,
                                       @Param("periodid") Integer periodid);

    /**
     * 更新学习状态
     */
    @Update("UPDATE user_study_period SET state = #{state}, createdAt = NOW() " +
            "WHERE userid = #{userid} AND periodid = #{periodid}")
    int updateStudyState(@Param("userid") Integer userid,
                        @Param("periodid") Integer periodid,
                        @Param("state") Integer state);

    /**
     * 插入新的学习记录
     */
    @Update("INSERT INTO user_study_period (userid, periodid, state, periodType, createdAt) " +
            "VALUES (#{userid}, #{periodid}, #{state}, #{periodType}, NOW())")
    int insertStudyRecord(@Param("userid") Integer userid,
                         @Param("periodid") Integer periodid,
                         @Param("state") Integer state,
                         @Param("periodType") Integer periodType);

    /**
     * 根据用户ID获取学习记录列表
     */
    @Select("SELECT * FROM user_study_period WHERE userid = #{userid} ORDER BY createdAt DESC")
    List<UserStudyPeriod> getUserStudyPeriods(@Param("userid") Integer userid);
} 